package com.mb.app.controller;

import com.mb.app.domain.TbAppInfo;
import com.mb.app.domain.TbProductClass;
import com.mb.app.service.ITbAppInfoService;
import com.mb.app.service.ITbProductClassService;
import com.mb.common.controller.BaseController;
import com.mb.common.utils.DateUtils;
import com.mb.common.utils.PageUtils;
import com.mb.common.utils.Query;
import com.mb.common.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zwl
 * @program
 * @description 分类管理
 * @date 2019-03-22 13:01
 **/
@Controller
@RequestMapping("/app/productClass")
@Slf4j
public class ProductClassController extends BaseController {
    
    @Autowired
    private ITbAppInfoService appInfoService;

    @Autowired
    private ITbProductClassService productClassService;

    @GetMapping("/index")
    String index(Model model){
        // 查询应用信息
        Map<String,Object> map = new HashMap<String,Object>();
        List<TbAppInfo> appInfos = appInfoService.getAppInfoList(map);
        model.addAttribute("appInfos",appInfos);
        return "/app/productClass/productClass_list";
    }


    @ResponseBody
    @GetMapping("/list")
    public PageUtils list(@RequestParam Map<String, Object> params) {
        // 查询列表数据
        // 时间截取
        String createTime = (String) params.get("createTime");
        if (StringUtils.isNotEmpty(createTime)) {
            params.put("startTime", DateUtils.parse(createTime.substring(0,10),DateUtils.DATE_PATTERN));
            params.put("endTime",createTime.substring(13,23)+" 23:59:59");
        }
        Query query = new Query(params);
        List<TbProductClass> list = productClassService.queryList(query);
        int total = productClassService.count(query);
        PageUtils pageUtils = new PageUtils(list, total);
        return pageUtils;
    }

    @GetMapping("/add")
    String add(Model model){
        // 查询应用信息
        Map<String,Object> map = new HashMap<String,Object>();
        List<TbAppInfo> appInfos = appInfoService.getAppInfoList(map);
        model.addAttribute("appInfos",appInfos);
        return "/app/productClass/productClass_add";
    }


    @ResponseBody
    @PostMapping("/save")
    public R save(TbProductClass TbProductClass, HttpServletRequest request){
        TbProductClass.setCreateUserId(getUserId());
        int result = productClassService.save(TbProductClass);
        return R.ok();
    }


    @ResponseBody
    @PostMapping("/remove")
    public R remove(@RequestParam("productClassId") Long productClassId, HttpServletRequest request){
        Long delUserId = getUserId();
        int result = productClassService.remove(productClassId,delUserId);
        return R.ok();
    }

    @GetMapping("/dtl/{productClassId}")
    String dtl(@PathVariable("productClassId") Long productClassId, Model model){
        model.addAttribute("productClassInfo",productClassService.getDtl(productClassId));
        return "/app/productClass/productClass_info";
    }


    @GetMapping("/edit/{productClassId}")
    String edit(@PathVariable("productClassId") Long productClassId, Model model){
        model.addAttribute("productClassInfo",productClassService.getDtl(productClassId));
        // 查询应用信息
        Map<String,Object> map = new HashMap<String,Object>();
        List<TbAppInfo> appInfos = appInfoService.getAppInfoList(map);
        model.addAttribute("appInfos",appInfos);
        return "/app/productClass/productClass_edit";
    }


    @ResponseBody
    @PostMapping("/update")
    public R update(TbProductClass TbProductClass, HttpServletRequest request){
        TbProductClass.setUpdateUserId(getUserId());
        int result = productClassService.update(TbProductClass);
        return R.ok();
    }

    /**
     * 渠道名称、渠道标识代码是否已存在
     */
    @PostMapping("/productClassNameExit")
    @ResponseBody
    boolean productClassNameExit(@RequestParam Map<String, Object> params) {
        // 存在，不通过，false
        return !productClassService.productClassNameExit(params);
    }

}
